package game;

import java.io.File;
import java.io.Serializable;
import java.util.Timer;
import java.util.TimerTask;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.TrueTypeFont;

import Netzwerk.DB_Zugriff;
import Netzwerk.GClient;
import Netzwerk.GServer;

import components.Bomb;
import components.Field;
import components.Fire;
import components.Item;
import components.Map;
import components.Space;

/**
 * 
 * This is the Player where the player and the hole game is control
 *
 */
public class Multiplayer extends BasicGameState implements Serializable{

	private static final long serialVersionUID = 1L;

	//Animationen
	int[] duration;
	Image sprite;

	/**
	 * The movements of the Player
	 */
	public  transient Image[][] walkUp, walkDown , walkRight, walkLeft, dead;
	public  static transient Animation[] movingUp;
	public  static transient Animation[] movingRight;
	public  static transient Animation[] movingDown;
	public  static transient Animation[] movingLeft;
	public  static transient Animation[] die;

	/**
	 * The different Images.
	 */
	public  static Image[] win, loose;
	public  static Image bomberi, fire, bomb, bomb_up, disease, fire_up, kick, space, speed_up, invincible, life_up, power, cup;
	public  static Image[] fields,grounds;
	public  static Image[] icons;

	/**
	 * The Map object that include Information about the playing map 
	 */
	public static components.Map map;
	public static int sizefield;
	public static int startx;
	public static int starty;
	public static float[][] startpos;

	/**
	 * Different Playerinformation to identify the different players.
	 */
	public  static int mod;
	public  int playerid;
	public  int playerzahl;
	public static String stat;

	/**
	 * Gameinformationen about the current game
	 */
	public  int winner;
	public  int maxWins;
	public transient Timer timer = new Timer();
	public static int seconds;
	public int time;
	public transient  Timer gameTime = new Timer();
	public static boolean revange;
	public static Font font;
	public static int anzahlLeben = 0;
	public boolean online;
	public int playing;
	public int[] com;

	/**
	 * Variablen for Help
	 */
	public int i = 0;
	public int j = 0;
	public float deltaHelpx = 0;
	public float deltaHelpy = 0;
	public int[][] itemField;

	/**
	 * Include the current Mapfile
	 */
	public transient File mapFile;

	/**
	 * 
	 */
	String IpAdresse;
	Multiplayer multi;
	public float[][] position = new float[2][2];

	/**
	 * Server and Client Object for communication over the network
	 */
	public  transient static GClient clientpl;
	public  transient static GServer serverpl; 

	/**
	 * No-Arg Constructor, is requiered for serialisierung with kryonet
	 */
	public Multiplayer(){
	}

	/**
	 * Constructor for the Client
	 * @param state
	 * @param client
	 * @param anzahlLeben
	 * @param maxWins
	 * @param playing
	 * @param map
	 * @param mod
	 * @param time
	 */
	public Multiplayer(int state, GClient client, int anzahlLeben, int maxWins, int playing, File map, int mod, int time){
		this.clientpl 				= client;
		this.playerid				= clientpl.getID()-1;
		this.playerzahl				= clientpl.getID();
		this.anzahlLeben			= anzahlLeben;
		this.maxWins		 		= maxWins;
		online 						= true;
		this.playing				= playing;
		this.mapFile				= map;
		this.mod					= mod;
		this.time					= time;
		this.seconds				= time*60;
		this.serverpl				= null;
		this.com					= null;
	}

	/**
	 * Constructor for Server
	 * @param state
	 * @param client
	 * @param server
	 * @param anzahlLeben
	 * @param maxWins
	 * @param playing
	 * @param map
	 * @param mod
	 * @param time
	 */
	public Multiplayer(int state, GClient client, GServer server, int anzahlLeben, int maxWins, int playing, File map, int mod, int time, String IpAdresse){
		clientpl 					= client;
		serverpl 					= server;
		playerid					= clientpl.getID()-1;
		playerzahl					= clientpl.getID();
		anzahlLeben 				= clientpl.getAnzahlLeben();								// clientpl.getAnzahLeben()-1, warum?
		maxWins		 				= clientpl.getMaxGewinne();
		online						= true;
		this.playing 				= playing;
		this.mapFile 				= map;
		this.mod	 				= mod;
		this.time	 				= time;
		this.seconds 				= time*60;
		this.com					= null;
		this.IpAdresse				= IpAdresse;

	}

	/**
	 * Constructor for Singleplayer
	 * @param state
	 * @param leben
	 * @param max
	 * @param playing
	 * @param mapFile
	 * @param mod
	 * @param time
	 */
	public Multiplayer(int state, int leben, int max, int playing, File mapFile, int mod, int time){
		playerid 					= 0;
		anzahlLeben					= leben;
		maxWins		 				= max;
		online 						= false;
		this.playing 				= playing;
		this.mapFile 				= mapFile;
		this.mod 					= mod;
		this.time					= time;
		seconds						= time*60;
		this.com					= null;
	}

	public Multiplayer(int state, int leben, int max, int playing, File mapFile, int mod, int time, int[] com){
		playerid 					= 0;
		anzahlLeben					= leben;
		maxWins		 				= max;
		online 						= false;
		this.playing 				= playing;
		this.mapFile 				= mapFile;
		this.mod 					= mod;
		this.time					= time;
		seconds						= time*60;
		this.com					= com;

	}	

	/**
	 * Slick 2D standard Method, init the gamecomponents
	 */
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

		gc.setAlwaysRender(true);

		/**
		 * Init Images
		 * 		IF-Condition decides which Picture is loaded in dependence of the current mod
		 */
		if(mod==4){
			fire = new Image("/Graphics/hearts.png");
			bomb = new Image("/Graphics/heart.png");
		}
		else if(mod==5){
			fire = new Image("/Graphics/fire.png");
			bomb = new Image("/Graphics/bomb.png");
		}
		else{
			fire = new Image("/Graphics/fire.png");
			bomb = new Image("/Graphics/bomb.png");
		}
		space = new Image("/Graphics/leer.png");
		cup = new Image("/Graphics/pokal.gif");

		//items
		bomb_up = new Image("/Graphics/Bomb_Up.png");
		disease = new Image("/Graphics/Disease.png");
		fire_up = new Image("/Graphics/Fire_Up.png");
		kick = new Image("/Graphics/Kick.png");
		speed_up = new Image("/Graphics/Speed_up.png");
		invincible = new Image("/Graphics/Invincible.png");
		life_up = new Image("/Graphics/Life_Up.png");
		power = new Image("/Graphics/power.png");

		//Field Graphics
		fields = new Image[]{new Image("/Graphics/field3.png"),new Image("/Graphics/leer.png"),new Image("/Graphics/field2.jpg"),
				new Image("/Graphics/field1.jpg"),new Image("/Graphics/field4.jpg"),new Image("/Graphics/field5.png"),
				new Image("/Graphics/field6.gif"),new Image("/Graphics/field7.png"),new Image("/Graphics/field8.png"),
				new Image("/Graphics/field9.png"),new Image("/Graphics/field10.png"),new Image("/Graphics/field11.gif")};

		//Ground Graphics
		grounds = new Image[]{new Image("/Graphics/leer.png"),new Image("/Graphics/ground1.gif"),new Image("/Graphics/ground2.gif"),
				new Image("/Graphics/ground3.jpg"),new Image("/Graphics/ground4.jpg"),new Image("/Graphics/ground5.jpg"),
				new Image("/Graphics/ground6.jpg"),new Image("/Graphics/ground7.jpg"),new Image("/Graphics/ground8.jpg")};		

		map = new components.Map(mapFile, anzahlLeben, this.playing);
		this.startpos = new float[map.bomber.length][2];
		this.sizefield = this.map.sizefield;

		startx=(Main.sizex-sizefield)/2;
		starty=(Main.sizey-sizefield)/2;

		/**
		 * Make the Animations
		 */
		duration = new int[]{300, 300, 300, 300};

		walkUp = new Image[map.bomber.length][4];
		walkRight = new Image[map.bomber.length][4];
		walkDown = new Image[map.bomber.length][4];
		walkLeft = new Image[map.bomber.length][4];
		dead = new Image[map.bomber.length][4];

		icons =new Image[map.bomber.length];
		win =new Image[map.bomber.length];
		loose =new Image[map.bomber.length];

		movingUp = new Animation[map.bomber.length];
		movingRight = new Animation[map.bomber.length];
		movingDown = new Animation[map.bomber.length];
		movingLeft =new Animation[map.bomber.length];
		die =new Animation[map.bomber.length];

		for(int i=0; i<map.bomber.length; i++){
			startpos[i][0]=map.getStarts(i)[0];
			startpos[i][1]=map.getStarts(i)[1];

			if(mod==4){
				sprite = new Image("/Graphics/bomberwoman_sprite.gif");

				walkUp[i] = new Image[]{sprite.getSubImage(0, i*37, 19, 37),sprite.getSubImage(21, i*37, 20, 37),sprite.getSubImage(0, i*37, 19, 37),sprite.getSubImage(42, i*37, 21, 37)};
				walkRight[i] = new Image[]{sprite.getSubImage(64, i*37, 19, 37),sprite.getSubImage(86, i*37, 20, 37),sprite.getSubImage(64, i*37, 19, 37),sprite.getSubImage(107, i*37, 21, 37)};
				walkLeft[i] = new Image[]{sprite.getSubImage(130, i*37, 19, 37),sprite.getSubImage(150, i*37, 20, 37),sprite.getSubImage(130, i*37, 19, 37),sprite.getSubImage(172, i*37, 21, 37)};
				walkDown[i] = new Image[]{sprite.getSubImage(194, i*37, 19, 37),sprite.getSubImage(214, i*37, 20, 37),sprite.getSubImage(194, i*37, 19, 37),sprite.getSubImage(233, i*37, 21, 37)};
				dead[i] = new Image[]{sprite.getSubImage(254, 4, 29, 32)};

				icons[i]= sprite.getSubImage(194, i*37, 19, 17);
				win[i]= sprite.getSubImage(194, i*37, 19, 37);
				loose[i]=sprite.getSubImage(254, 4, 29, 32);

				movingUp[i] = new Animation(walkUp[i], duration, false);
				movingRight[i] = new Animation(walkRight[i], duration, false);
				movingDown[i] = new Animation(walkDown[i], duration, false);
				movingLeft[i] = new Animation(walkLeft[i], duration, false);
				die[i] = new Animation(dead[i], new int[]{500}, false);

				map.bomber[i].animation=new Animation(new Image[]{movingDown[i].getCurrentFrame()}, 190, false);

			}
			else if (mod==5){
				map.bomber[i].img=new Image("/Graphics/zombie.png");
			}
			else{
				//Animationen
				sprite = new Image("/Graphics/bomberman_sprite.gif");

				walkUp[i] = new Image[]{sprite.getSubImage(0, i*54, 17, 30),sprite.getSubImage(17, i*54, 17, 30),sprite.getSubImage(0, i*54, 17, 30),sprite.getSubImage(34, i*54, 17, 30)};
				walkRight[i] = new Image[]{sprite.getSubImage(52, i*54, 17, 30),sprite.getSubImage(69, i*54, 17, 30),sprite.getSubImage(52, i*54, 17, 30),sprite.getSubImage(88, i*54, 17, 30)};
				walkDown[i] = new Image[]{sprite.getSubImage(106, i*54, 17, 30),sprite.getSubImage(122, i*54, 17, 30),sprite.getSubImage(106, i*54, 17, 30),sprite.getSubImage(139, i*54, 17, 30)};
				walkLeft[i] = new Image[]{sprite.getSubImage(157, i*54, 17, 30),sprite.getSubImage(175, i*54, 17, 30),sprite.getSubImage(157, i*54, 17, 30),sprite.getSubImage(194, i*54, 17, 30)};
				dead[i] = new Image[]{sprite.getSubImage(0, 29+i*54, 17, 27),sprite.getSubImage(18, 29+i*54, 17, 27),sprite.getSubImage(32, 29+i*54, 10, 27), sprite.getSubImage(42, 29+i*54, 10, 27)};

				icons[i]=sprite.getSubImage(106, i*54, 17, 15);
				win[i]= sprite.getSubImage(211, 11+i*54, 24, 34);
				loose[i]=sprite.getSubImage(235, 11+i*54, 24, 34);

				movingUp[i] = new Animation(walkUp[i], duration, false);
				movingRight[i] = new Animation(walkRight[i], duration, false);
				movingDown[i] = new Animation(walkDown[i], duration, false);
				movingLeft[i] = new Animation(walkLeft[i], duration, false);
				die[i] = new Animation(dead[i], duration, false);

				map.bomber[i].animation=new Animation(new Image[]{movingDown[i].getCurrentFrame()}, 190, false);
			}
		}

		if(mod == 2){
			revange=true;
		}
		else{
			revange=false;
		}
		setItems();
		stat="run";

		font = new TrueTypeFont (new java.awt.Font("Baskerville Old Face",0,30),false );


		gameTime= new Timer();
		gameTime.schedule(new TimerTask() {
			public void run() {
				seconds--;
			};
		}, 1000, 1000);

		if(this.online){
			clientpl.bomber		= this.map.bomber;
		}
		if(this.serverpl!= null && this.online == true){
			serverpl.bomberAll 	= this.map.bomber;
			this.serverpl.sendItemField(itemField);
		}

		if(this.com!=null){
			for(int i=1;i<this.com.length;i++){
				if(this.com[i]==1){
					map.bomber[i].com=true;
				}
				else{
					map.bomber[i].lifes=-1;
					map.bomber[i].dead=true;
				}
			}
		}

	}

	@Override
	/**
	 * Render the game
	 */
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

		g.setColor(Color.white);
		if(mod==4){
			g.setColor(Color.pink);
			g.fillRect(0, 0, Main.sizex, Main.sizex);
			g.setColor(Color.white);
		}

		for(int i=0;i<map.size;i++){
			for(int j=0;j<map.size;j++){
				map.ground[i][j].img.draw((float)startx+j*map.blocksize, (float)starty+i*map.blocksize, 30, 30);

			}
		}
		for(int i=0;i<map.size;i++){
			for(int j=0;j<map.size;j++){
				map.field[i][j].img.draw((float)startx+map.field[i][j].position[0]*map.blocksize,
						(float)starty+map.field[i][j].position[1]*map.blocksize, 30, 30);

			}
		}

		for(int i=0;i<map.bomber.length;i++){
			if(map.bomber[i].lifes>=0 || !map.bomber[i].dead){
				map.bomber[i].animation.draw(startx+map.bomber[i].position[0]*map.blocksize, starty+map.bomber[i].position[1]*map.blocksize, 28,30);
				if(this.online){
					clientpl.bomber[i].animation = map.bomber[i].animation;
				}
			}
		}

		g.setFont(font);
		String s;
		if((seconds-(60*(seconds/60)))>=10){
			s=seconds/60+":"+(seconds-(60*(seconds/60)));
		}
		else{
			s=seconds/60+":0"+(seconds-(60*(seconds/60)));
		}
		g.drawString(s, 100, 2);
		for(int i=0; i<map.bomber.length;i++){
			icons[i].draw(200+i*100, 2 , 25, 25);
			g.drawString(map.bomber[i].wins+"", 230+i*100, 2);
		}

		if(stat.equals("draw")){
			g.drawString("DRAW", 100, 100);
		}
		else if(stat.equals("win")){
			g.drawString("Player "+(winner+1)+ " wins", 100, 100);
		}
	}

	@Override
	/**
	 * update the game
	 * registered all changes and input
	 */
	public void update(final GameContainer gc, final StateBasedGame sbg, int delta) throws SlickException {

		Input in = gc.getInput();
		for(int i=0; i<map.bomber.length; i++){
			movingUp[i].update(delta);
			movingDown[i].update(delta);
			movingRight[i].update(delta);
			movingLeft[i].update(delta);
			die[i].update(delta);
		}

		if(seconds<=0 && stat.equals("run")){
			gameTime.cancel();
			gameTime.purge();
			stat="draw";
			winner=(-1);

			timer= new Timer();
			timer.schedule(new TimerTask() {
				public void run() {
					stat="result";
					timer.cancel();
					timer.purge();
				};
			}, 4000);

		}

		if(stat.equals("result")){
			if(winner>=0){
				map.bomber[winner].wins++;
			}
			/**
			 * render stopped and between income is called
			 */
			this.pauseRender();
			sbg.addState(new Result(7, winner, win, loose, maxWins, mod, map));
			sbg.getState(7).init(gc, sbg);
			sbg.enterState(7);

			/**
			 * field reset
			 */
			timer.cancel();
			timer.purge();
			timer= new Timer();
			timer.schedule(new TimerTask() {
				public void run() {

					boolean finish=false;

					for(int i=0;i<map.bomber.length;i++){

						map.bomber[i].lifes=1;
						map.bomber[i].position[0]=startpos[i][0];
						map.bomber[i].position[1]=startpos[i][1];
						map.bomber[i].noItems(anzahlLeben);
						map.bomber[i].animation=new Animation(new Image[]{game.Multiplayer.movingDown[map.bomber[i].id].getImage(0)}, 190, false);
						map.bomber[i].animation.stop();
						map.bomber[i].animation.setCurrentFrame(0);

						if(map.bomber[i].wins>=maxWins){
							finish = true;
						}
					}
					map.setFieldBack();
					setItems();

					if(finish){
						sbg.enterState(0);
						timer.cancel();
						timer.purge();

					}
					else{

						stat="run";

						seconds=time*60;
						gameTime= new Timer();
						gameTime.schedule(new TimerTask() {
							public void run() {
								seconds--;
							};
						}, 1000, 1000);

						sbg.enterState(2);
						setRenderPaused(false);

						timer.cancel();
						timer.purge();
					}
				};
			}, 4000);
		}

		if(stat.equals("run")){

			/**
			 * determined winner
			 */
			int alive=0;
			for(int i=0;i<map.bomber.length;i++){
				if(map.bomber[i].lifes>=0){
					alive++;
					winner=i;
				}
			}

			if(alive==0){
				gameTime.cancel();
				gameTime.purge();
				stat="draw";
				timer= new Timer();
				timer.schedule(new TimerTask() {
					public void run() {
						stat="result";
						timer.cancel();
						timer.purge();
					};
				}, 4000);
			}
			else if(alive==1){
				gameTime.cancel();
				gameTime.purge();
				stat="win";
				timer= new Timer();
				timer.schedule(new TimerTask() {
					public void run() {
						stat="result";
						timer.cancel();
						timer.purge();
					};
				}, 4000);
			}

			if(in.isKeyDown(Input.KEY_ESCAPE)){
				sbg.enterState(0);
			}

			/**
			 *  checked whether Bomber must lay down a bomb
			 */
			for(int i=0;i<map.bomber.length;i++){
				if(this.online && clientpl.bomber[i].bomblay == true && i != this.playerid){
					this.map.field[clientpl.bomber[i].bomby][clientpl.bomber[i].bombx]=new Bomb(new float[]{clientpl.bomber[i].bombx, clientpl.bomber[i].bomby}, this.map.bomber[i], this.map.bomber[i].time);
					clientpl.bomber[i].bomblay = false;
				}
				if(map.bomber[i].forcedToBomb){
					map.bomber[i].setBomb();
				}
			}

			/**
			 * Checked movement, whether bomber stands in fire
			 */


			deltaHelpx = map.bomber[this.playerid].position[0];
			deltaHelpy = map.bomber[this.playerid].position[1];

			this.map.bomber[this.playerid].move(in, delta, 1);


			deltaHelpx = deltaHelpx - map.bomber[this.playerid].position[0];
			deltaHelpy = deltaHelpy - map.bomber[this.playerid].position[1];

			if(this.online && deltaHelpx != 0 || this.online && deltaHelpy != 0 ){
				positionUpdate();
			}

			if(!online && this.com==null){
				map.bomber[1].move(in, delta, 2);
			}
			else if(!online && this.com!=null){
				for(int i=1;i<this.playing;i++){
					map.bomber[i].move(in, delta);
				}
			}
			for(int i=0;i<map.bomber.length;i++){


				if(map.field[Math.round(map.bomber[i].position[1])][Math.round(map.bomber[i].position[0])].type.equals("fire")
						&& map.bomber[i].lifes>=0
						&& !map.bomber[i].invincible){
					map.bomber[i].die();

				}

				/**
				 * item activat
				 */
				else if(map.field[Math.round(map.bomber[i].position[1])][Math.round(map.bomber[i].position[0])].type.equals("item")){

					Field helpfeld=
							map.field[Math.round(map.bomber[i].position[1])][Math.round(map.bomber[i].position[0])];//.effect(map.bomber[i]);
					map.field[Math.round(map.bomber[i].position[1])][Math.round(map.bomber[i].position[0])].type="space";
					map.field[Math.round(map.bomber[i].position[1])][Math.round(map.bomber[i].position[0])]=
							new Space(map.field[Math.round(map.bomber[i].position[1])][Math.round(map.bomber[i].position[0])].position);
					helpfeld.effect(i);
				}
			}

			/**
			 * Bombs movement calculation
			 */
			for(int i=0;i<map.size;i++){
				for(int j=0;j<map.size;j++){

					if(map.field[i][j].type.equals("bomb")){

						/**
						 * right movement
						 */
						if(map.field[i][j].speedx>0 && ( nextFieldWalkable("right", j, i)|| map.field[i][j].position[0]-j<0)){
							map.field[i][j].position[0]+=map.field[i][j].speedx*delta;
						}
						/**
						 * left movement
						 */
						else if(map.field[i][j].speedx<0 && (nextFieldWalkable("left", j, i) || map.field[i][j].position[0]-j>0)){
							map.field[i][j].position[0]+=map.field[i][j].speedx*delta;
						}
						/**
						 * movement up
						 */
						else if(map.field[i][j].speedy<0 && (nextFieldWalkable("up", j, i) || map.field[i][j].position[1]-i>0)){
							map.field[i][j].position[1]+=map.field[i][j].speedy*delta;
						}
						/**
						 * movement down
						 */
						else if(map.field[i][j].speedy>0 && (nextFieldWalkable("down", j, i) || map.field[i][j].position[1]-i<0)){
							map.field[i][j].position[1]+=map.field[i][j].speedy*delta;
						}
						else{
							map.field[i][j].speedx=0;
							map.field[i][j].speedy=0;
						}

						/**
						 * Bomb field replace
						 */
						int posx = Math.round(map.field[i][j].position[0]);
						int posy = Math.round(map.field[i][j].position[1]);
						if(posx!=j || posy!=i){
							if(map.field[posy][posx].type.equals("fire")){

								map.field[posy][posx].delete();
								map.field[posy][posx]=map.field[i][j];
								map.field[i][j]=new Space(new float[]{j,i});
								map.field[posy][posx].delete();
							}
							else{
								map.field[posy][posx]=map.field[i][j];
								map.field[i][j]=new Space(new float[]{j,i});
							}
						}
					}
				}
			}
		}

		for(int i=0;i<map.bomber.length;i++){
			if(this.online){
				clientpl.bomber[i].animationPlayer();
			}
		}
		if(this.online){
			this.map.bomber = clientpl.bomber;
		}

		if(this.online && this.clientpl.isConnected() == false){
			sbg.enterState(0);
		}
	}
	@Override
	public int getID() {
		return 2;
	}

	/**
	 * Method for updating Positions of all Players over network
	 */
	private void positionUpdate(){
		clientpl.sendMyMap(this.map.bomber[this.playerid]);
		clientpl.bomber[this.playerid].bomblay = false;
	}

	/**
	 * 
	 * @param s
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean nextFieldWalkable(String s, int x, int y){
		if(s.equals("right")){
			if(x==this.map.size-1){
				return false;
			}
			else{
				return map.field[y][x+1].walk ;
			}

		}
		else if(s.equals("left")){
			if(x==0){
				return false;
			}
			else{
				return map.field[y][x-1].walk ;
			}

		}
		else if(s.equals("up")){
			if(y==0){
				return false;
			}
			else{
				return map.field[y-1][x].walk ;
			}
		}
		else if(s.equals("down")){
			if(y==map.size-1){
				return false;
			}
			else{
				return map.field[y+1][x].walk ;
			}

		}
		else{return false;}
	}

	/**
	 * 
	 * @param s
	 * @param x
	 * @param y
	 * @param t
	 * @return
	 */
	public boolean nextFieldType(String s, int x, int y, String t){
		if(s.equals("right")){
			if(x==this.map.size-1){
				return false;
			}
			else{
				return map.field[y][x+1].type.equals(t);
			}

		}
		else if(s.equals("left")){
			if(x==0){
				return false;
			}
			else{
				return map.field[y][x-1].type.equals(t) ;
			}

		}
		else if(s.equals("up")){
			if(y==0){
				return false;
			}
			else{
				return map.field[y-1][x].type.equals(t) ;
			}
		}
		else if(s.equals("down")){
			if(y==map.size-1){
				return false;
			}
			else{
				return map.field[y+1][x].type.equals(t) ;
			}

		}
		else{return false;}
	}

	/**
	 * 
	 */
	public void setItems(){
		int anzahl;
		int count;
		int x,y;
		int breaker=0;
		if(!this.online || this.serverpl != null){
			itemField = new int[map.size][map.size];
		}else if(this.online && this.serverpl == null){
			itemField = this.clientpl.getItemField();
		}

		if(this.serverpl != null || !this.online){
			//classic
			//Feuer items
			anzahl=map.bomber.length* ((int) map.size/4);
			count=0;
			while(count<anzahl){
				x=(int)Math.round(Math.random()*(map.size-1));
				y=(int)Math.round(Math.random()*(map.size-1));
				if(map.field[y][x].type.equals("block") && map.field[y][x].destroy && map.field[y][x].item == null){
					map.field[y][x].item=new Item(map.field[y][x].position,"fire_up",fire_up);
					itemField[y][x] =  1;
					count++;
					breaker=0;
				}
				else{
					breaker++;
				}

				if(breaker>=50){
					break;
				}
			}

			//Bomb items
			anzahl=map.bomber.length*((int) map.size/4);
			count=0;
			while(count<anzahl){
				x=(int)Math.round(Math.random()*(map.size-1));
				y=(int)Math.round(Math.random()*(map.size-1));
				if(map.field[y][x].type.equals("block") && map.field[y][x].destroy && map.field[y][x].item == null){
					map.field[y][x].item=new Item(map.field[y][x].position,"bomb_up",bomb_up);
					itemField[y][x] =  2;
					count++;
					breaker=0;
				}
				else{
					breaker++;
				}

				if(breaker>=50){
					break;
				}
			}

			//Speed items
			anzahl=map.bomber.length*((int) map.size/4);
			count=0;
			while(count<anzahl){
				x=(int)Math.round(Math.random()*(map.size-1));
				y=(int)Math.round(Math.random()*(map.size-1));
				if(map.field[y][x].type.equals("block") && map.field[y][x].destroy && map.field[y][x].item == null){
					map.field[y][x].item=new Item(map.field[y][x].position,"speed_up",speed_up);
					itemField[y][x] =  3;
					count++;
					breaker=0;
				}
				else{
					breaker++;
				}

				if(breaker>=50){
					break;
				}
			}
			//Kick items
			anzahl=map.bomber.length-1;
			count=0;
			while(count<anzahl){
				x=(int)Math.round(Math.random()*(map.size-1));
				y=(int)Math.round(Math.random()*(map.size-1));
				if(map.field[y][x].type.equals("block") && map.field[y][x].destroy && map.field[y][x].item == null){
					map.field[y][x].item=new Item(map.field[y][x].position,"kick",kick);
					itemField[y][x] =  4;
					count++;
					breaker=0;
				}
				else{
					breaker++;
				}

				if(breaker>=50){
					break;
				}
			}
			//Disease items
			anzahl=map.bomber.length*((int) map.size/4);
			count=0;
			while(count<anzahl){
				x=(int)Math.round(Math.random()*(map.size-1));
				y=(int)Math.round(Math.random()*(map.size-1));
				if(map.field[y][x].type.equals("block") && map.field[y][x].destroy && map.field[y][x].item == null){
					map.field[y][x].item=new Item(map.field[y][x].position,"disease",disease);
					itemField[y][x] =  5;
					count++;
					breaker=0;
				}
				else{
					breaker++;
				}

				if(breaker>=50){
					break;
				}
			}

			if(mod==3){
				//Invincible items
				anzahl=map.bomber.length-2;
				if(anzahl<=0){
					anzahl=1;
				}
				count=0;
				while(count<anzahl){
					x=(int)Math.round(Math.random()*(map.size-1));
					y=(int)Math.round(Math.random()*(map.size-1));
					if(map.field[y][x].type.equals("block") && map.field[y][x].destroy && map.field[y][x].item == null){
						map.field[y][x].item=new Item(map.field[y][x].position,"invincible",invincible);
						itemField[y][x] =  6;
						count++;
						breaker=0;
					}
					else{
						breaker++;
					}

					if(breaker>=50){
						break;
					}
				}
				//Life Up items
				anzahl=map.bomber.length-2;
				if(anzahl<=0){
					anzahl=1;
				}
				count=0;
				while(count<anzahl){
					x=(int)Math.round(Math.random()*(map.size-1));
					y=(int)Math.round(Math.random()*(map.size-1));
					if(map.field[y][x].type.equals("block") && map.field[y][x].destroy && map.field[y][x].item == null){
						map.field[y][x].item=new Item(map.field[y][x].position,"life",life_up);
						itemField[y][x] =  7;
						count++;
						breaker=0;
					}
					else{
						breaker++;
					}

					if(breaker>=50){
						break;
					}
				}
				//Powerbomb items
				anzahl=map.bomber.length-1;
				count=0;
				while(count<anzahl){
					x=(int)Math.round(Math.random()*(map.size-1));
					y=(int)Math.round(Math.random()*(map.size-1));
					if(map.field[y][x].type.equals("block") && map.field[y][x].destroy && map.field[y][x].item == null){
						map.field[y][x].item=new Item(map.field[y][x].position,"power",power);
						itemField[y][x] =  8;
						count++;
						breaker=0;
					}
					else{
						breaker++;
					}

					if(breaker>=50){
						break;
					}
				}
			}
		}else{
			for(int k = 0; k < map.size; k++){
				for(int l = 0; l < map.size; l++){
					if(itemField[k][l] == 1){
						map.field[k][l].item = new Item(map.field[k][l].position,"fire_up",fire_up);
					}else if(itemField[k][l] == 2){
						map.field[k][l].item=new Item(map.field[k][l].position,"bomb_up",bomb_up);
					}else if(itemField[k][l] == 3){
						map.field[k][l].item=new Item(map.field[k][l].position,"speed_up",speed_up);
					}else if(itemField[k][l] == 4){
						map.field[k][l].item=new Item(map.field[k][l].position,"kick",kick);
					}else if(itemField[k][l] == 5){
						map.field[k][l].item=new Item(map.field[k][l].position,"disease",disease);
					}else if(itemField[k][l] == 6){
						map.field[k][l].item=new Item(map.field[k][l].position,"invincible",invincible);
					}else if(itemField[k][l] == 7){
						map.field[k][l].item=new Item(map.field[k][l].position,"life",life_up);
					}else if(itemField[k][l] == 8){
						map.field[k][l].item=new Item(map.field[k][l].position,"power",power);
					}
				}
			}
		}
	}
}

